using System;
using ParserGenerator.Extensions;

namespace ParserGenerator.TableCompression
{
    [Serializable]
    public struct CompactTableCell : IEquatable<CompactTableCell>, IComparable<CompactTableCell>, IComparable
    {
        public int Column { get; }
        public int Value { get; }

        internal CompactTableCell(int column, int value)
        {
            if (column < 0)
                throw new ArgumentOutOfRangeException(nameof(column), SR.ColumnMustBeNonNegative);
            if (value < 0)
                throw new ArgumentOutOfRangeException(nameof(value), SR.ValueMustBeNonNegative);
            Column = column;
            Value = value;
        }

        public override int GetHashCode()
        {
            return ICollectionExtensions.CombineHashCodes(Column, Value);
        }

        public bool Equals(CompactTableCell other)
        {
            return Column == other.Column && Value == other.Value;
        }

        public override bool Equals(object obj)
        {
            return obj is CompactTableCell && Equals((CompactTableCell)obj);
        }

        public int CompareTo(CompactTableCell other)
        {
            int t = Column.CompareTo(other.Column);
            if (t != 0)
                return t;
            return Value.CompareTo(other.Value);
        }

        public int CompareTo(object obj)
        {
            if (obj == null)
                return 1;
            else if (obj is CompactTableCell)
                return CompareTo((CompactTableCell)obj);
            else
                throw new ArgumentException(SR.ArgumentMustBeCompactTableCell, nameof(obj));
        }

        public override string ToString()
        {
            return Column + " -> " + Value;
        }
    }
}
